package com.teachoa.web;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.teachoa.bean.Course;
import com.teachoa.bean.Uncertain;
import com.teachoa.util.IDUtil;

public class UncertainController extends BaseController {
	
	/**
	 * 候选项目
	 */
	public void index() {
		int pageNo = getParaToInt("pageNo", 1);
		setAttr("projectPage", Uncertain.dao.getUncertainPageByTeacherId(pageNo, getCurrentUserId()));
		renderJsp("project.jsp");
	}
	
	public void load() {
		String id = getPara();
		if(StringUtils.isNotEmpty(id))
			setAttr("project", Uncertain.dao.findById(id));
//		List<Process> list = getSnakerEngine().process().getProcesss(new QueryFilter());
//		setAttr("flows", list);
		setAttr("courses", Course.dao.findAll());
		renderJsp("project_add.jsp");
	}
	
	@Before(Tx.class)
	public void save() {
		Uncertain pro = getModel(Uncertain.class);
		String id = pro.getStr("id");
		try {
			if(StringUtils.isEmpty(id)) {
				pro.set("id", IDUtil.getId());
				pro.set("user_id", super.getCurrentUserId());
				pro.set("user_name", super.getCurrentUserName());
				pro.set("status", 0);
				pro.set("create_time", new Date());
				pro.save();
			} else
				pro.update();
			renderJson(success("保存成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("保存失败！！"));
		}
	}
	
	public void turnpage() {
		int pageNo = getParaToInt("pageNo", 1);
		setAttr("projectPage", Uncertain.dao.getUncertainPageByTeacherId(pageNo, getCurrentUserId()));
		renderJsp("project_table.jsp");
	}
	
	@Before(Tx.class)
	public void del() {
		try {
			Uncertain.dao.deleteById(getPara());
			renderJson(success("删除成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("删除失败！！"));
		}
	}
	
	public void student() {
		String studentId = getCurrentUserId();
		int pageNo = getParaToInt("pageNo", 1);
		setAttr("projectPage", Uncertain.dao.getUncertainPageByStudentId(pageNo, studentId));
		renderJsp("project_student.jsp");
	}
	
	public void studentPage() {
		String studentId = getCurrentUserId();
		int pageNo = getParaToInt("pageNo", 1);
		setAttr("projectPage", Uncertain.dao.getUncertainPageByStudentId(pageNo, studentId));
		renderJsp("project_student_table.jsp");
	}
	
	public void upload() {
		try {
			UploadFile file = getFile("file");
			renderJson(success("/upload/"+file.getFileName()));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("上传失败！！！"));
		}
	}

}
